package CI;

import java.util.ArrayList;

public class Procedure {

	private ArrayList<Object> _params;
	private ArrayList<Object> _body;
	private Environment _environment;

	public Procedure (ArrayList<Object> params, ArrayList<Object> body, Environment env)
	{
		this._params = params;
		this._body = body;
		this._environment = env;

	}
	public ArrayList<Object> getParams()
	{
		return this._params;
	}


	public ArrayList<Object> getBody(){
		return this._body;
	}

	public Environment getEnvironment(){
		return this._environment;        
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return String.format("<Procedure %s / %s>", this._params.toString(), this._body.toString());
	}



	public Boolean isLambda(Object expr)
	{
		return Evaluator.isSpecialForm(expr, "lambda");
	}

	public Procedure evalLambda(Object expr, Environment env) throws EvalError
	{
		assert isLambda(expr);
		ArrayList<Object> listExpr = null;
		if(expr instanceof ArrayList<?>)
		{
			listExpr = (ArrayList<Object>)expr;
		}
		if(listExpr.size()  != 3)
		{
			throw new EvalError(String.format("Bad lambda expression: %s", expr.toString()));
		}

		return new Procedure((ArrayList<Object>)listExpr.get(1), (ArrayList<Object>)listExpr.get(2), env);
	}
}
